{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "838f8252-2136-41de-81d8-8ab6ef197c73",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from transformers import T5Tokenizer, T5ForConditionalGeneration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0dd65569-829d-4343-b6bc-7eea38a77fa4",
   "metadata": {},
   "outputs": [],
   "source": [
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")       \n",
    "tokenizer = T5Tokenizer.from_pretrained('t5-small', legacy=False, device=device)\n",
    "model = T5ForConditionalGeneration.from_pretrained('t5-small', return_dict=True, device_map=device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8b0c370d-4943-4db3-9a4a-41deb604ff4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "premise = \"The corner coffee shop serves the most awesome coffee I have ever had.\"\n",
    "hypothesis = \"I love the coffee served by the corner coffee shop.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "51200ab1-f3c7-4ebd-958d-35f16aef2deb",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_ids = tokenizer(\"mnli premise: \" + premise + \" hypothesis: \" + hypothesis, return_tensors=\"pt\").input_ids\n",
    "entailment_ids = model.generate(input_ids.to(device), max_new_tokens=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "32e287e3-a5f8-4735-8f94-50dc5d886d29",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "entailment\n"
     ]
    }
   ],
   "source": [
    "prediction = tokenizer.decode(entailment_ids[0], skip_special_tokens=True, device=device)\n",
    "print(prediction)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chapter09",
   "language": "python",
   "name": "chapter09"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
